package com.bst.base.service.impl;

import com.bst.base.domain.BaseNote;
import com.bst.base.service.IBaseNoteService;
import com.bst.base.vo.BaseNoteParamVO;
import com.bst.common.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component("baseQuartzTask")
public class BaseQuartzTaskServiceImpl {

    @Autowired
    IBaseNoteService baseNoteService;
    @Value("${ruoyi.status.note}")
    private String noteStatus;

    private static final Integer ADD_NUM = 100;

    /**
     * 短信发送，小于当前时间的未发送信息直接发送
     * @param jobId
     * @param jobLogId
     * @throws ParseException
     */
    public synchronized void sendMsg(Long jobId,Long jobLogId) throws ParseException {
        if(!"start".equals(noteStatus)) {
            return;
        }
        String tomorrow = DateUtil.FORMAT_YYYY_MM_DD.format(DateUtil.calculate(new Date(), Calendar.DAY_OF_MONTH, 1));
        BaseNoteParamVO note = new BaseNoteParamVO();
        note.setDtNoteExpect(DateUtil.FORMAT_YYYY_MM_DD.parse(tomorrow));
        note.setEuNote(0L);
        note.setFgAll(0);
        List<BaseNote> notes =baseNoteService.selectBaseNoteList(note);
        List<BaseNote> sunNotes;
        if(notes.size()>0) {
            int start=0;int end = ADD_NUM;
            int total = notes.size();
            while(total>start) {
                sunNotes = notes.subList(start,total<end?total:end);
                baseNoteService.sendMsg2(sunNotes);
                baseNoteService.updateBatchBaseNote(sunNotes);
                start+=ADD_NUM;end+=ADD_NUM;
                if(total>start) {
                    try {
                        Thread.sleep(1000*60*10); //休眠10分钟
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
}
